package com.linkage.jz.bankruptcy.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;

import java.util.ArrayList;

public class PopupMenuPublicPro extends PopupWindow implements OnClickListener {

	private Activity activity;
	private View popView;

	private View v_item1;
	private View v_item2;
	private View v_item3;
	private View v_item4;
	private View v_item5;
	private View v_item6;
	private View v_item7;
	private View v_item8;
	private View v_item9;
	private View v_item10;
	private TextView tv_item1;
	private TextView tv_item2;
	private TextView tv_item3;
	private TextView tv_item4;
	private TextView tv_item5;
	private TextView tv_item6;
	private TextView tv_item7;
	private TextView tv_item8;
	private TextView tv_item9;
	private TextView tv_item10;




	private OnItemClickListener onItemClickListener;

	/**
	 *
	 * @author ywl5320 枚举，用于区分选择了哪个选项
	 */
	public enum MENUITEM {
		ITEM1, ITEM2,ITEM3, ITEM4,ITEM5, ITEM6,ITEM7, ITEM8,ITEM9, ITEM10
	}

	public PopupMenuPublicPro(Activity activity) {
		super(activity);
		this.activity = activity;
		LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popView = inflater.inflate(R.layout.popup_menu_public_detail, null);// 加载菜单布局文件
		this.setContentView(popView);// 把布局文件添加到popupwindow中
		this.setWidth(LayoutParams.WRAP_CONTENT);// 设置菜单的宽度（需要和菜单于右边距的距离搭配，可以自己调到合适的位置）
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);// 获取焦点
		this.setTouchable(true); // 设置PopupWindow可触摸
		this.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);

		// 获取选项卡
		v_item1 = popView.findViewById(R.id.ly_item1);
		v_item2 = popView.findViewById(R.id.ly_item2);
		v_item3 = popView.findViewById(R.id.ly_item3);
		v_item4 = popView.findViewById(R.id.ly_item4);
		v_item5 = popView.findViewById(R.id.ly_item5);
		v_item6 = popView.findViewById(R.id.ly_item6);
		v_item7 = popView.findViewById(R.id.ly_item7);
		v_item8 = popView.findViewById(R.id.ly_item8);
		v_item9 = popView.findViewById(R.id.ly_item9);
		v_item10 = popView.findViewById(R.id.ly_item10);

		tv_item1 = popView.findViewById(R.id.tv_item1);
		tv_item2 = popView.findViewById(R.id.tv_item2);
		tv_item3 = popView.findViewById(R.id.tv_item3);
		tv_item4 = popView.findViewById(R.id.tv_item4);
		tv_item5 = popView.findViewById(R.id.tv_item5);
		tv_item6 = popView.findViewById(R.id.tv_item6);
		tv_item7 = popView.findViewById(R.id.tv_item7);
		tv_item8 = popView.findViewById(R.id.tv_item8);
		tv_item9 = popView.findViewById(R.id.tv_item9);
		tv_item10 = popView.findViewById(R.id.tv_item10);


		// 添加监听
		v_item1.setOnClickListener(this);
		v_item2.setOnClickListener(this);
		v_item3.setOnClickListener(this);
		v_item4.setOnClickListener(this);
		v_item5.setOnClickListener(this);
		v_item6.setOnClickListener(this);
		v_item7.setOnClickListener(this);
		v_item8.setOnClickListener(this);
		v_item9.setOnClickListener(this);
		v_item10.setOnClickListener(this);

	}

	public void initData(ArrayList<String> strAry) {
		v_item1.setVisibility(View.GONE);
		v_item2.setVisibility(View.GONE);
		v_item3.setVisibility(View.GONE);
		v_item4.setVisibility(View.GONE);
		v_item5.setVisibility(View.GONE);
		v_item6.setVisibility(View.GONE);
		v_item7.setVisibility(View.GONE);
		v_item8.setVisibility(View.GONE);
		v_item9.setVisibility(View.GONE);
		v_item10.setVisibility(View.GONE);

		for (int i = 0; i < strAry.size(); i++) {
			String str = strAry.get(i);
			switch (i) {
				case 0:
					v_item1.setVisibility(View.VISIBLE);
					tv_item1.setText(str);
					break;
				case 1:
					v_item2.setVisibility(View.VISIBLE);
					tv_item2.setText(str);
					break;
				case 2:
					v_item3.setVisibility(View.VISIBLE);
					tv_item3.setText(str);
					break;
				case 3:
					v_item4.setVisibility(View.VISIBLE);
					tv_item4.setText(str);
					break;
				case 4:
					v_item5.setVisibility(View.VISIBLE);
					tv_item5.setText(str);
					break;
				case 5:
					v_item6.setVisibility(View.VISIBLE);
					tv_item6.setText(str);
					break;
				case 6:
					v_item7.setVisibility(View.VISIBLE);
					tv_item7.setText(str);
					break;
				case 7:
					v_item8.setVisibility(View.VISIBLE);
					tv_item8.setText(str);
					break;
				case 8:
					v_item9.setVisibility(View.VISIBLE);
					tv_item9.setText(str);
					break;
				case 9:
					v_item10.setVisibility(View.VISIBLE);
					tv_item10.setText(str);
					break;

				default:
					break;


			}
		}


	}

	public void initData(String strItem1,String strItem2,String strItem3,String strItem4,String strItem5,String strItem6,String strItem7,String strItem8,String strItem9,String strItem10)
	{
		if("".equalsIgnoreCase(strItem1))
		{
			v_item1.setVisibility(View.GONE);
		}else{
			tv_item1.setText(strItem1);
		}

		if("".equalsIgnoreCase(strItem2))
		{
			v_item2.setVisibility(View.GONE);
		}else{
			tv_item2.setText(strItem2);
		}

		if("".equalsIgnoreCase(strItem3))
		{
			v_item3.setVisibility(View.GONE);
		}else{
			tv_item3.setText(strItem3);
		}

		if("".equalsIgnoreCase(strItem4))
		{
			v_item4.setVisibility(View.GONE);
		}else{
			tv_item4.setText(strItem4);
		}

		if("".equalsIgnoreCase(strItem5))
		{
			v_item5.setVisibility(View.GONE);
		}else{
			tv_item5.setText(strItem5);
		}

		if("".equalsIgnoreCase(strItem6))
		{
			v_item6.setVisibility(View.GONE);
		}else{
			tv_item6.setText(strItem6);
		}

		if("".equalsIgnoreCase(strItem7))
		{
			v_item7.setVisibility(View.GONE);
		}else{
			tv_item7.setText(strItem7);
		}
		if("".equalsIgnoreCase(strItem8))
		{
			v_item8.setVisibility(View.GONE);
		}else{
			tv_item8.setText(strItem8);
		}

		if("".equalsIgnoreCase(strItem9))
		{
			v_item9.setVisibility(View.GONE);
		}else{
			tv_item9.setText(strItem9);
		}

		if("".equalsIgnoreCase(strItem10))
		{
			v_item10.setVisibility(View.GONE);
		}else{
			tv_item10.setText(strItem10);
		}

	}


	/**
	 * 设置显示的位置
	 * 
	 * @param resourId
	 *            这里的x,y值自己调整可以
	 */
	public void showLocation(int resourId) {
		showAsDropDown(activity.findViewById(resourId), dip2px(activity, 0),
				dip2px(activity, -8));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		MENUITEM menuitem = null;
		String str = "";
		if (v == v_item1) {
			menuitem = MENUITEM.ITEM1;
			str = tv_item1.getText().toString().trim();
		}
		else if (v == v_item2) {
			menuitem = MENUITEM.ITEM2;
			str = tv_item2.getText().toString().trim();
		}
		else if (v == v_item3) {
			menuitem = MENUITEM.ITEM3;
			str = tv_item3.getText().toString().trim();
		}else if (v == v_item4) {
			menuitem = MENUITEM.ITEM4;
			str = tv_item4.getText().toString().trim();
		}else if (v == v_item5) {
			menuitem = MENUITEM.ITEM5;
			str = tv_item5.getText().toString().trim();
		}else if (v == v_item6) {
			menuitem = MENUITEM.ITEM6;
			str = tv_item6.getText().toString().trim();
		}
		else if (v == v_item7) {
			menuitem = MENUITEM.ITEM7;
			str = tv_item7.getText().toString().trim();
		}
		else if (v == v_item8) {
			menuitem = MENUITEM.ITEM8;
			str = tv_item8.getText().toString().trim();
		}
		else if (v == v_item9) {
			menuitem = MENUITEM.ITEM9;
			str = tv_item9.getText().toString().trim();
		}
		else if (v == v_item10) {
			menuitem = MENUITEM.ITEM10;
			str = tv_item10.getText().toString().trim();
		}
		if (onItemClickListener != null) {
			onItemClickListener.onClick(menuitem, str);
		}
		dismiss();
	}

	// dip转换为px
	public int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	// 点击监听接口
	public interface OnItemClickListener {
		public void onClick(MENUITEM item, String str);
	}

	// 设置监听
	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

}
